//
//  BLCommentInputView.m
//  BasketBallLeagues
//
//  Created by kenshin on 5/19/17.
//  Copyright © 2017 kenshin. All rights reserved.
//

#import "BLCommentInputView.h"
#import "JCTextView.h"

@interface BLCommentInputView ()<UITextViewDelegate>

@property (nonatomic, strong) JCTextView *textView;
@property (nonatomic, strong) UIButton *sendBtn;
@property (nonatomic, strong) UIView *lineView;

@end

@implementation BLCommentInputView

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        _textView = [[JCTextView alloc] init];
        _textView.layer.masksToBounds = YES;
        _textView.layer.cornerRadius = 4.f;
        _textView.layer.borderWidth = 0.5f;
        _textView.layer.borderColor = [BLColorConfig darkGrayColor].CGColor;
        _textView.returnKeyType = UIReturnKeySend;
        _textView.delegate = self;
        [_textView setPlaceholder:@"评论"];
        _textView.maxVisibleNumberOfLines = 5;
        @weakify(self);
        _textView.heightChangedBlock = ^(CGFloat height) {
            @strongify(self);
            if (self.heightChangedBlock) {
                self.heightChangedBlock(height + 12);
            }
        };
        [self addSubview:_textView];
        
        UIButton *btn = [UIButton new];
        [btn setTitle:@"发送" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14.f];
        btn.backgroundColor = [UIColor blueColor];
        btn.layer.cornerRadius = 4;
        btn.clipsToBounds = YES;
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        _sendBtn = btn;
        [self addSubview:_sendBtn];
        
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [BLColorConfig lineColor];
        [self addSubview:_lineView];
        
        CGFloat height = 1.f/[UIScreen mainScreen].scale;
        [_lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(height);
            make.leading.top.trailing.mas_equalTo(0);
        }];
        [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.mas_equalTo(16);
            make.top.mas_equalTo(6);
            make.bottom.mas_equalTo(-6);
            make.trailing.mas_equalTo(_sendBtn.mas_leading).mas_offset(-6);
        }];
        [_sendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(-6);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(30);
            make.trailing.mas_equalTo(-6);
        }];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification
                                                   object:nil];
    }
    return self;
}

- (void)sendComment {
    NSString *commentString = [self.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (commentString.length < 1) {
        [self ks_showToast:@"内容不能为空"];
        return;
    }
    
    if (self.sendCommentBlock) {
        self.sendCommentBlock(self.textView.text);
    }
    self.textView.text = nil;
    [self.textView setNeedsDisplay];
    
}

- (void)btnAction:(UIButton *)btn
{
    [self sendComment];
}

- (void)keyboardWillShow:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    if (self.keyboardBlock) {
        self.keyboardBlock(duration, (curve << 16 | UIViewAnimationOptionBeginFromCurrentState), CGRectGetHeight(endFrame));
    }
}

- (void)keyboardWillHide:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    if (self.keyboardBlock) {
        self.keyboardBlock(duration, (curve << 16 | UIViewAnimationOptionBeginFromCurrentState), 0);
    }
}

#pragma mark - UITextViewDelegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if (self.textWillEditingBlock) {
        self.textWillEditingBlock();
    }
    return YES;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if (text.length < 1 && ![textView hasText]) {
        return NO;
    }
    NSString *commentString = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if ([text isEqualToString:@"\n"]) {
        if (commentString.length < 1) {
            return NO;
        }
        if (self.sendCommentBlock) {
            self.sendCommentBlock(textView.text);
        }
        self.textView.text = nil;
        [self.textView setNeedsDisplay];
        return NO;
    }
    return YES;
}


- (void)prepareInput:(NSString *)replyText {
    [_textView becomeFirstResponder];
    [_textView setPlaceholder:[NSString stringWithFormat:@"回复@%@：", replyText]];
}

- (void)clearText {
    [_textView setPlaceholder:@"评论"];
}
@end
